package com.bbx.taxi.client.Task;

import android.app.Activity;
import android.os.AsyncTask;

import com.bbx.androidapi.util.SharedPreUtil;
import com.bbx.androidapi.util.ToastUtil;
import com.bbx.api.sdk.model.base.BaseRequest;
import com.bbx.api.sdk.model.official.passanger.Officail_OrderInfoBuild;
import com.bbx.api.sdk.model.official.passanger.Return.Officail_OrderInfo;
import com.bbx.api.sdk.model.passanger.WeightOrVolumnBuild;
import com.bbx.api.sdk.model.passanger.Return.OrderDetails;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.Locations;
import com.bbx.api.sdk.model.passanger.Return.MyOrderList.MyOrderList;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.sdk.net.official.passanger.Officail_OrderInfoNet;
import com.bbx.api.sdk.net.passeger.conn.CancelReasonListNet;
import com.bbx.api.sdk.net.passeger.conn.OrderDetailNet;
import com.bbx.api.sdk.net.passeger.conn.WeightNet;
import com.bbx.taxi.client.Task.BaseAsyncTask;
import com.bbx.api.util.SharedPreEncryptUtil;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.official.R;
import com.bbx.taxi.client.Task.DetailsOfGoodsTask.OnFinishListener;

/**
 *     主要用于判断订单状态
 *
 */
public class OrderDetailTask {
	private Activity context;
	private MyApplication mApplication;
	public static OrderDetailTask instance;
	public MyOrderDetailTask mMyOrderDetailTask;
	public interface OnOrderDetailFinishListener {
		/**
		 *订单详情请求完毕返回mOrderDetails为空则失败
		 */
		public void onOrderDetailFinish(Officail_OrderInfo mOfficail_OrderInfo);
	}

	static OnOrderDetailFinishListener mOnOrderDetailFinishListener = null;

	public static void setOnOrderDetailFinishListener(OnOrderDetailFinishListener l) {
		mOnOrderDetailFinishListener = l;
	}
	public static OrderDetailTask getInstance(Activity context,
			MyApplication mApplication,String order_id) {
		instance = new OrderDetailTask(context, mApplication,order_id);
		return instance;
	}
	public OrderDetailTask(Activity context, MyApplication mApplication,String order_id) {
		this.context = context;
		this.mApplication = mApplication;
		getOrderDetail(order_id);
	}
	/**
	 * 获取订单详情
	 */
	public void getOrderDetail(String order_id) {
		Officail_OrderInfoBuild mOrderDetail = new Officail_OrderInfoBuild(context);
		mOrderDetail.passenger_id = mApplication.getUid();
		mOrderDetail.order_id = order_id;
		mMyOrderDetailTask = new MyOrderDetailTask(context, mOrderDetail);
		mMyOrderDetailTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}

	/**
	 * 订单详情
	 */
	private class MyOrderDetailTask extends BaseAsyncTask {

		private Officail_OrderInfoBuild mOrderDetail;

		public MyOrderDetailTask(Activity context, Officail_OrderInfoBuild mOrderDetail) {
			super(context);
			this.mOrderDetail = mOrderDetail;
		}

		@Override
		public void onSuccess(Object object) {
			if (object != null) {
				Officail_OrderInfo mOfficail_OrderInfo = (Officail_OrderInfo) object;
				if (mOfficail_OrderInfo != null&&mOfficail_OrderInfo.getInfo()!=null&&mOfficail_OrderInfo.getInfo().getOrder_id()!=null) {
					if (mOnOrderDetailFinishListener != null) {
						mOnOrderDetailFinishListener.onOrderDetailFinish(mOfficail_OrderInfo);
					}
				}else{
					if (mOnOrderDetailFinishListener != null) {
						mOnOrderDetailFinishListener.onOrderDetailFinish(null);
					}
				}

			} else {
				ToastUtil.showToast(context, R.string.no_network);
				if (mOnOrderDetailFinishListener != null) {
					mOnOrderDetailFinishListener.onOrderDetailFinish(null);
				}
			}
		}

		@Override
		public void onFailed(int status, String msg) {
			ToastUtil.showToast(context, R.string.no_network);
			if (mOnOrderDetailFinishListener != null) {
				mOnOrderDetailFinishListener.onOrderDetailFinish(null);
			}
		}

		@Override
		protected Object doInBackground(Object... params) {
			// TODO Auto-generated method stub
			String json = new JsonBuild().setModel(mOrderDetail).getJson();
			return new Officail_OrderInfoNet(context, json);
		}
	}

}
